require("judge");

function reLogin ()
  
  local finished = false;
  while not finished do
    if isPage("探索页面") then
      tap(109,107);--回到庭院
      mSleep(2000);
      
      finished = false;
      
    elseif isPage("庭院") then
      tap(100,95);--点击头像
      mSleep(1000);
      tap(1440,334);--点击用户中心
      mSleep(1000);
      tap(2068,75);--点击切换账号
      mSleep(2000);
      tap(1251,594);--点击登录
      mSleep(5000);
      
      finished = true;
      
    end
    
  end
end


--自动换狗粮
function checkAndChangeFull(task) 
  
  --判断是否需要进行检查
  if autoChangeFull == false then
    return;
  end
  
  local shouldChange = false;
  if task.name == "妖怪发现" then
    shouldChange = true;
  elseif task.name == "只做封任务" then
    shouldChange = true;
  elseif task.name == "空闲时接受邀请" and task.canChangeFull == true then
    shouldChange = true;
  end
  
  if not shouldChange then
    return;
  end
  
  
  --获取两个数组用于保存位置
  --观战位置
  local watchArea = nil;
  --出战位置
  local fightArea = nil;
  
  --查询出战区域
  if autoChangeFullType == 0 then
    fightArea =	{ 10,400,1420,1100 };
  elseif autoChangeFullType == 1 then
    fightArea = { 10,400,1020,1100 };
  elseif autoChangeFullType == 2 then
    fightArea = { 10,400,706,1100 };
  else
    fightArea = { 10,400,11,401 };
  end
  
  --查询观战区域
  watchArea = { 528,177,773,400 };
  
  local changeFullKind = "更换出战";
  if hasFindFullInArea(fightArea) then
    printLog("点击出战区域");
    changeFullKind = "更换出战";
    tap(832,936);
    tap(832,936);
    mSleep(2000);
  elseif hasFindFullInArea(watchArea) then
    printLog("点击观战区域, 需要双击");
    changeFullKind = "更换观战";
    tap(515, 440);
    tap(515, 440);
    mSleep(2000);
  else
    printLog("没有需要更换的狗粮");
    return;
  end
  
  --选择需要替换的狗粮 
  tap(118,946);--展开
  mSleep(500);
  if changeType == 0 then
    tap(123,556);--N
  elseif changeType == 1 then
    tap(317,610);--R
  elseif changeType == 2 then
    tap(463,756);--SR
  elseif changeType == 3 then
    tap(507,945);--SSR
  elseif changeType == 4 then
    tap(457,1144);--全部
  end
  mSleep(500);
  
  --查询三次满的位置
  local hasFind = false;
  local findArea = {};
  
  if changeFullKind == "更换出战" then
    
    if task.makeTeam == true then
      --五个中排在后两个的式神
      
      if autoChangeFullType == 0 then
        findArea = {500,308,1741,610};
        
      elseif autoChangeFullType == 1 then
        findArea = {1500,308,1741,610};
        
      else
        findArea = {1,1,5,5};--一个很小的区域，保证无法找到
      end
      
    else 
      
      --单独的三个式神
      if autoChangeFullType == 0 then
        findArea = {250,100,2000,560};
      elseif autoChangeFullType == 1 then
        findArea = {900,100,2000,560};
        
      elseif autoChangeFullType == 2 then
        findArea = {1560,100,2000,560};
      else
        findArea = {1,1,5,5};--一个很小的区域，保证无法找到
      end
      
    end
    
  else
    --观战
    findArea = {450,8,1550,650};
  end
  
  
  for tmpi=1,3 do
    local x ,y = findFullInArea(findArea);
    if x > -1 then
      hasFind = true;
      
      printLog("找打了满了的狗粮,x:"..x.."  y:"..y.."， 开始进行选择狗粮");
      --换上没有满的，且不是红黑蓝达摩的狗粮
      moveUnfullToPoint(x,y, scrollTimes);
      
    end		
  end
  
  --更换狗粮 部分需要更换的狗粮太高，找不到满的字样，解决策略是：如果进入了更换页面，如果能找到满字，则换满的， 找不到则全部更换
  if hasFind == false then
    --五个位置都更换
    if changeFullKind == "更换出战" then
      
      if task.makeTeam == true then
        --做队长邀请别人 排在后边的两个式神
        if autoChangeFullType == 0 then
          moveUnfullToPoint(589,450, scrollTimes);--队长位置
          moveUnfullToPoint(1623,450, scrollTimes);--3号位置
          
        elseif autoChangeFullType == 1 then
          moveUnfullToPoint(1623,450, scrollTimes);--3号位置
        end
        
      else
        --单独的三个式神
        --五个中排在前三个的式神
        if autoChangeFullType == 0 then
          moveUnfullToPoint(366,400, scrollTimes);--队长位置
          moveUnfullToPoint(1109,400, scrollTimes);--2号位置
          moveUnfullToPoint(1868,400, scrollTimes);--3号位置
          
        elseif autoChangeFullType == 1 then
          moveUnfullToPoint(1109,400, scrollTimes);--2号位置
          moveUnfullToPoint(1868,400, scrollTimes);--3号位置
          
        elseif autoChangeFullType == 2 then
          moveUnfullToPoint(1868,400, scrollTimes);--3号位置
          
        end
      end
      
    else
      moveUnfullToPoint(693,415, scrollTimes);--4号位置
      moveUnfullToPoint(1376,309, scrollTimes);--5号位置
      
    end
  end
  
  if isPage("更换式神") then
    tap(64, 50);--退出
    mSleep(1000);
  end
end


--自动更换狗粮时，把更换位置的式神调整为普通
function changePositionNormalAttack(task) 
  
  --判断是否需要调整
  --判断是否需要进行检查
  if autoChangeFull == false then
    return;
  end
  
  local shouldChange = false;
  if task.name == "妖怪发现" then
    shouldChange = true;
  elseif task.name == "只做封任务" then
    shouldChange = true;
  elseif task.name == "空闲时接受邀请" and task.canChangeFull == true then
    shouldChange = true;
  end
  
  if not shouldChange then
    return;
  end
  
  --需要把妖术修改为普通的区域
  local checkArea = nil;
  --单人
  if task.makeTeam == false then
        if autoChangeFullType == 0 then
      checkArea = { 1430,1133,1970,1232 };
    elseif autoChangeFullType == 1 then
      checkArea = { 1430,1133,1770,1232 };
      
    elseif autoChangeFullType == 2 then
      checkArea = { 1430,1133,1570,1232 };
    else
      checkArea = { 1430,1133,1431,1134 };--选择一个不可能需要改变的区域
    end
    
  elseif task.makeTeam == true then
    --队长
    if autoChangeFullType == 0 then
      checkArea = { 1620,1133,1970,1232 };
    elseif autoChangeFullType == 1 then
      checkArea = { 1620,1133,1770,1232 };
		else
      checkArea = { 1620,1133,1621,1134 };--选择一个不可能需要改变的区域
    end
  else
    --队员
    if autoChangeFullType == 0 then
      checkArea = { 1630,1133,2160,1232 };
    elseif autoChangeFullType == 1 then
      checkArea = { 1630,1133,1950,1232 };
      
    elseif autoChangeFullType == 2 then
      checkArea = { 1630,1133,1750,1232 };
    else
      checkArea = { 1630,1133,1631,1134 };--选择一个不可能需要改变的区域
    end
  end
  
	
	--根据获得区域，进行判断是否有妖术字样，有则点击一下。
	--加载自定义字库
  local lines = {};
	--妖术 0xf7f6f6-0x444444
	lines[1] = "0FE7FCFF0E000000000000000000000010020040180300E01C0700C0380E0380F01C03002000000000000000000000000000000000000000000000000000000000000000000000000000000000000003007FFFFFFFC0000000000000000000000000C0180380300$妖术$10.10.725$43";
	local dic = createOcrDict(lines);
  
  local point = ocrText(dic, checkArea[1],checkArea[2],checkArea[3],checkArea[4], {"0xf7f6f6-0x444444"}, 95,1,0);
  
  if #point ~= 0 then  --如果找到符合条件的点
    for var = 1,#point do
      
			tap(point[var].x, point[var].y);
			mSleep(500);
    end
  end
end


--战斗时自动集火中间式神
function autoAim(task) 
  local shouldAutoAim = true;
  
  if shouldAutoAim == false then
    return;
  end
  
  --是自动时点击固定位置
  if task.name == "妖怪发现" then
    
  elseif task.name == "自动觉醒" then
    
  elseif task.name == "自动御魂" then
    
  elseif task.name == "妖气封印" then
    
  end
  
end

